import 'package:dio/dio.dart';
import 'package:fsharing/core/models/bank.dart';
import 'package:fsharing/core/services/api.dart';
import 'package:fsharing/core/viewmodels/view_model.dart';

class WithdrawCreateModel extends ViewModel {
  final Api api;

  MapEntry<String, dynamic> _account;
  Bank _bank;
  String amount;

  String _errorMessage;

  WithdrawCreateModel(this.api) : super();

  MapEntry<String, dynamic> get account => _account;

  set account(MapEntry<String, dynamic> value) {
    _account = value;
    notifyListeners();
  }

  Bank get bank => _bank;

  set bank(Bank value) {
    _bank = value;
    notifyListeners();
  }

  String get errorMessage => _errorMessage;

  Future<ViewState> withdrawCreate() async {
    viewState = ViewState.busy;
    try {
      var params = {'accountType': _account?.key, 'bankAccountId': _bank?.id, 'amount': amount};
      await api.withdrawCreate(params);
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }
}
